package com.manor.common.dialog

import android.content.Context
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.OnSelectListener
import com.lxj.xpopup.interfaces.SimpleCallback
import com.lxj.xpopup.interfaces.XPopupCallback
import com.lxj.xpopup.util.XPopupUtils
import com.manor.common.R
import com.manor.common.utils.setGone
import kotlinx.android.synthetic.main.dialog_h_confirm.view.*
import kotlin.math.roundToInt

/**
 *


 * @date  2020/7/21  17:28
 * @version 1.0
 */
class HConfirmDialog(context: Context, val listener: OnSelectListener, val title: String, val content: String, val
cancel: String, val confirm: String, val needTitle: Boolean,  val needContent: Boolean, val needCancel: Boolean, val
needConfirm: Boolean, val dialogWid: Float, val callBack: XPopupCallback) : CenterPopupView(context) {

    companion object {
        fun load(context: Context, listener: OnSelectListener, title: String = "", content: String, cancel: String = "取消",
                 confirm: String = "确定", needTitle: Boolean = true,needContent: Boolean, needCancel: Boolean = true, needConfirm: Boolean = true,
                 callBack: XPopupCallback = SimpleCallback(), canDispatch: Boolean = true, canBack: Boolean = true,
                 dialogWid: Float = 0.75f) {
            XPopup.Builder(context)
                    .dismissOnTouchOutside(canDispatch)
                    .dismissOnBackPressed(canBack)
                    .setPopupCallback(callBack)
                    .asCustom(HConfirmDialog(context, listener, title, content, cancel, confirm, needTitle,
                            needContent, needCancel, needConfirm, dialogWid, callBack))
                    .show()
        }
    }

    override fun getImplLayoutId(): Int = R.layout.dialog_h_confirm


    override fun onCreate() {
        super.onCreate()
        tvTitle.setGone(!needTitle)
        tvContent.setGone(!needContent)
        tvConfirm.setGone(!needConfirm)
        tvCancel.setGone(!needCancel)
        viewBottom.setGone(!needCancel)
        tvTitle.text = title
        tvContent.text = content
        tvConfirm.text = confirm
        tvCancel.text = cancel
        tvConfirm.setOnClickListener {
            dismiss()
            listener.onSelect(0, "") }
        tvCancel.setOnClickListener { dismiss() }

    }


    override fun getMaxWidth(): Int {
        return (XPopupUtils.getWindowWidth(context) * dialogWid).roundToInt()
    }


}